package com.yuan.blog.controller.system;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.blog.core.utils.R;
import com.yuan.blog.entity.SystemUserEntity;
import com.yuan.blog.service.ISystemUserService;
import com.yuan.blog.service.impl.SystemUserServiceImpl;
import com.yuan.blog.vo.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebServlet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/SystemUserController")
public class SystemUserController{

    @Autowired
    private ISystemUserService systemUserService;


    @GetMapping("/systemUserPage/{curPage}")
    public String systemUserPage(MyPage<SystemUserEntity> page, @PathVariable("curPage") Integer curPage,ModelMap map) throws Exception{
        systemUserService.systemUserPage(page,curPage);
        map.put("page",page);
        return "system/sysUser/sysUserList";
    }

    @GetMapping("/list")
    public String list(ModelMap map) throws Exception {

        // 1.查询数据
        List<SystemUserEntity> list = systemUserService.list();

        // 2.把数据放到Map中
        map.put("sysUserList", list);

        // 3.跳转到视图

        return "system/sysUser/sysUserList";
    }


    @PostMapping(value = "/add")
    @ResponseBody
    public R add(@RequestBody SystemUserEntity systemUser) throws Exception {
        systemUser.setStatus(0); // 默认值是0
        systemUser.setCreateTime(new Date());
        boolean add = systemUserService.save(systemUser);
        return R.out(add);
    }

    @GetMapping("/systemUserById/{id}")
    public String systemUserById(@PathVariable("id") Integer id,ModelMap map){
        SystemUserEntity systemUser = systemUserService.getById(id);
        map.put("systemUser", systemUser);
        return "system/sysUser/sysUserUpdate";

    }

    @PostMapping("/update")
    @ResponseBody
    public R update(@RequestBody SystemUserEntity systemUser) throws Exception {
        boolean update = systemUserService.updateById(systemUser);
        return R.out(update);
    }


    @GetMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable("id") Integer id){
        boolean delete = systemUserService.removeById(id);
        return R.out(delete );
    }

    //批量删除，状态变为1
    @PostMapping("/batchDel")
    @ResponseBody
    public R batchDel(@RequestBody List<Integer> list) throws Exception {
        boolean del = systemUserService.removeBatchByIds(list);
        return R.out(del);
    }
}
